/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmGUI.java
 *
 * Created on Nov 14, 2011, 8:35:53 AM
 */

package gestor;

import java.util.Map.Entry;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import util.GUI;


public class FrmGUI extends javax.swing.JFrame {
	private Agente agente = null;

    /** Creates new form FrmGUI */
    public FrmGUI(Agente a) {
		this.agente = a;
        initComponents();

		GUI.centerWindowOnScreen(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNome = new javax.swing.JLabel();
        pnlTerminar = new javax.swing.JPanel();
        cmdTerminar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblRegisto = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(agente.nome);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        lblNome.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblNome.setText("Agente gestor: " + agente.nome);
        getContentPane().add(lblNome, java.awt.BorderLayout.NORTH);

        cmdTerminar.setText("Terminar");
        cmdTerminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdTerminarActionPerformed(evt);
            }
        });
        pnlTerminar.add(cmdTerminar);

        getContentPane().add(pnlTerminar, java.awt.BorderLayout.SOUTH);

        tblRegisto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Agente", "Positivo", "Negativo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblRegisto);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
		close();
	}//GEN-LAST:event_formWindowClosing

	private void cmdTerminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTerminarActionPerformed
		close();
	}//GEN-LAST:event_cmdTerminarActionPerformed

	private void close() {
		if(JOptionPane.showConfirmDialog(null, "Tem a certeza que quer terminar o agente gestor?", agente.nome, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
			agente.stop();
			dispose();
		}
	}

	protected void refreshList() {
		DefaultTableModel modelo = (DefaultTableModel)tblRegisto.getModel();
		while(modelo.getRowCount() > 0) {
			modelo.removeRow(0);
		}
		
		for(Entry<String, Reputacao> x: agente.registo.entrySet()) {
			Object[] values = new Object[3];
			values[0] = x.getKey();
			values[1] = x.getValue().getPositive();
			values[2] = x.getValue().getNegative();
			modelo.addRow(values);
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdTerminar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNome;
    private javax.swing.JPanel pnlTerminar;
    private javax.swing.JTable tblRegisto;
    // End of variables declaration//GEN-END:variables

}
